package edu.software.PBL_Management_System.comment.repository;

import edu.software.PBL_Management_System.entity.Comment;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface CommentRepository extends JpaRepository<Comment,Long> {
    // 根据需求ID查找所有评论
    List<Comment> findByDemandIdOrderByCreatedTimeDesc(Long demandId);

    // 根据用户ID查找收到的评论
    List<Comment> findByDemandIdInOrderByCreatedTimeDesc(List<Long> demandIds);

    // 查找某个评论的所有回复
    List<Comment> findByParentIdOrderByCreatedTimeDesc(Long parentId);

    // 查找用户未读的评论数量
    Long countByUserIdAndIsReadFalse(Long userId);

    // 查找特定需求下的评论数量
    Long countByDemandId(Long demandId);

    // 查找特定需求下的未读评论数量
    Long countByDemandIdAndIsReadFalse(Long demandId);

    // 查找多个需求的最新评论
    @Query("SELECT c FROM Comment c WHERE c.demandId IN :demandIds AND c.createdTime = (SELECT MAX(c2.createdTime) FROM Comment c2 WHERE c2.demandId = c.demandId)")
    List<Comment> findLatestCommentsByDemandIds(@Param("demandIds") List<Long> demandIds);

    // 查找专家评论过的所有需求ID
    @Query("SELECT DISTINCT c.demandId FROM Comment c WHERE c.userId = :userId")
    List<Long> findDistinctDemandIdsByUserId(@Param("userId") Long userId);
}
